package com.camus.utils.xls;

import java.util.List;
import java.util.function.Predicate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

public class CommonExcelReaderLister<T> extends AnalysisEventListener<T> {

	private static final Logger log = LoggerFactory.getLogger(CommonExcelReaderLister.class);

	private List<T> resultList;

	/**
	 * 过滤规则
	 */
	private Predicate<T> filter;

	public CommonExcelReaderLister(List<T> resultList, Predicate<T> filter) {
		this.resultList = resultList;
		this.filter = filter;
	}

	@Override
	public void invoke(T data, AnalysisContext context) {
		// 读取满足条件的
		if (filter.test(data)) {
			resultList.add(data);
		}
	}

	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
		log.info("所有数据解析完成！");
	}
}
